package com.hezhi.project.module.grade.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hezhi.project.module.grade.domain.Grade;
import com.hezhi.project.module.grade.service.IGradeService;
import com.hezhi.framework.web.controller.BaseController;
import com.hezhi.framework.web.page.TableDataInfo;
import com.hezhi.framework.web.domain.AjaxResult;

/**
 * 年级 信息操作处理
 * 
 * @author hezhi
 * @date 2018-08-06
 */
@Controller
@RequestMapping("/module/grade")
public class GradeController extends BaseController {
	private String prefix = "module/grade";

	@Autowired
	private IGradeService gradeService;

	@RequiresPermissions("module:grade:view")
	@GetMapping()
	public String grade() {
		return prefix + "/grade";
	}

	/**
	 * 查询年级列表
	 */
	@RequiresPermissions("module:grade:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Grade grade) {
		startPage();
		List<Grade> list = gradeService.selectGradeList(grade);
		return getDataTable(list);
	}

	/**
	 * 新增年级
	 */
	@RequiresPermissions("module:grade:add")
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 修改年级
	 */
	@RequiresPermissions("module:grade:edit")
	@GetMapping("/edit/{gradeNumber}")
	public String edit(@PathVariable("gradeNumber") String gradeNumber, Model model) {
		Grade grade = gradeService.selectGradeById(gradeNumber);
		model.addAttribute("grade", grade);
		return prefix + "/edit";
	}

	/**
	 * 保存年级
	 */
	@RequiresPermissions("module:grade:save")
	@PostMapping("/save")
	@ResponseBody
	public AjaxResult save(Grade grade) {
		if (gradeService.saveGrade(grade) > 0) {
			return success();
		}
		return error();
	}

	/**
	 * 删除年级
	 */
	@RequiresPermissions("module:grade:remove")
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		int rows = gradeService.deleteGradeByIds(ids);
		if (rows > 0) {
			return success();
		}
		return error();
	}

	/**
	 * 验证年级编码唯一
	 * 
	 * @param grade
	 * @return
	 */
	@PostMapping("/checkGradeNumber")
	@ResponseBody
	public String checkGradeNumber(Grade grade) {
		String s = "0";
		if (grade != null && !"".equals(grade.getGradeNumber()) && grade.getGradeNumber() != null) {
			if (gradeService.selectGradeById(grade.getGradeNumber()) != null) {
				s = "1";
			}
		}
		return s;
	}

}
